<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1200" destroy-on-close @closed="$emit('closed')" align-center="true" :close-on-click-modal="false">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right" style="height:700px;overflow:auto">
           <div class="title">报修信息</div>
           <el-row :gutter="1">
                <el-col :span="8">
                    <el-form-item label="设备代码" prop="code">
                        <el-input v-model="detail.code" placeholder="设备代码" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="设备名称" prop="title">
                        <el-input v-model="detail.title" placeholder="设备名称" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="设备类别" prop="class_title">
                        <el-input v-model="detail.class_title" placeholder="设备类别" readonly></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="8">
                    <el-form-item label="故障类别" prop="fault_title">
                        <el-input v-model="detail.fault_title" placeholder="故障类别" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="紧急程度" prop="urgent_name">
                        <el-input v-model="detail.urgent_name" placeholder="紧急程度" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="报修人" prop="maintenance_admin_name">
                        <el-input v-model="detail.maintenance_admin_name" placeholder="报修人" readonly></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="8">
                    <el-form-item label="报修时间" prop="maintenance_create_time">
                        <el-date-picker v-model="detail.maintenance_create_time" :value-format="'YYYY-MM-DD'"  style="width:100%" type="date" disabled />
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="故障描述" prop="remark">
                        <el-input v-model="detail.remark" placeholder="故障描述" readonly></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="8">
                    <el-form-item label="响应人" prop="work_admin_name">
                        <el-input v-model="detail.work_admin_name" placeholder="响应人" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="响应时间" prop="work_create_time">
                        <el-date-picker v-model="detail.work_create_time" :value-format="'YYYY-MM-DD'"  style="width:100%" type="date" disabled />
                    </el-form-item>
                </el-col>
            </el-row>
            <div class="title">维修汇报信息</div>
            <el-row :gutter="1">
                <el-col :span="8">
                    <el-form-item label="维修人" prop="user_name">
                        <el-input v-model="detail.user_name" placeholder="维修人" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="维修开始时间" prop="start_date">
                        <el-date-picker v-model="form.start_date" type="datetime" placeholder="开始时间" format="YYYY-MM-DD HH:mm:ss" date-format="MMM DD, YYYY" time-format="HH:mm" style="width:100%" />
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="维修结束时间" prop="end_date">
                        <el-date-picker v-model="form.end_date" type="datetime" placeholder="结束时间" format="YYYY-MM-DD HH:mm:ss" date-format="MMM DD, YYYY" time-format="HH:mm" style="width:100%" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="8">
                    <el-form-item label="协作人" prop="assist_id">
                        <el-select v-model="form.assist_id"  filterable style="width: 100%" placeholder="请选择协作人" clearable multiple>
                            <el-option v-for="item in humList" :key="item.id" :label="item.name" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="故障原因" prop="check_admin">
                        <el-input v-model="form.task_reason" placeholder="故障原因"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="维修内容" prop="task_handle">
                        <el-input v-model="form.task_handle" placeholder="维修内容"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="8">
                    <el-form-item label="验证人" prop="check_admin">
                        <el-select v-model="form.check_admin" style="width: 100%" placeholder="验证人">
                            <el-option v-for="item in humList" :key="item.id" :label="item.name" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="验证时间" prop="check_time">
                        <el-input v-model="form.check_time" placeholder="验证时间"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item label="备注" prop="task_remark">
                <el-input v-model="form.task_remark" type="textarea" clearable></el-input>
            </el-form-item>
            <el-row :gutter="1">
                <el-col :span="8">
                    <el-form-item label="故障图片" prop="img">
                        <sc-upload v-model="detail.img" draggable disabled></sc-upload>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="维修图片" prop="task_img">
                        <sc-upload v-model="form.task_img" draggable></sc-upload>
                    </el-form-item>
                </el-col>
            </el-row>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增维修工单',
				edit: '维修汇报',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				id: "",
				// flow_id: "",
				assist_id: "",
                task_reason:"",
                task_handle:"",
                task_img:"",
                task_remark:"",
                check_admin:"",
                start_date:"",
                end_date:"",
                remark:"",
			},
            detail:{
                code:"",
                title:"",
                class_title:"",
                fault_title:"",
                urgent_name:"",
                user_neme:"",
                create_time:"",
                admin_name:"",
                user_name:"",
                work_admin_name:"",
                work_create_time:"",
                maintenance_admin_name:"",
                maintenance_create_time:"",
            },
			//验证规则
			rules: {
				// ware_name: [
				// 	{ required: true, message: '请输入维修工单名称' }
				// ],
				// ware_code: [
				// 	{ required: true, message: '请输入维修工单编号' }
				// ],
			},
            flowList:[],//审批
            humList:[],//人员
		}
	},
	mounted() {
        // this.getFlow()
        this.getHum()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					res = await this.$API.device.deviceTask.add.post(this.form);
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("汇报成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		async setData(data) {
            var res = await this.$API.device.deviceTask.detail.post({id:data.id});
            console.log(res);
            this.form.start_date = res.data.start_date
            this.form.end_date = res.data.end_date
            this.form.remark = res.data.remark
            if(res.data.assist_id){
                let a  = (res.data.assist_id).split(",")
                let b = []
                a.forEach(item => {
                    b.push(Number(item))
                });
                this.form.assist_id = b
            }
            this.form.task_reason = res.data.task_reason
            this.form.check_admin = res.data.check_admin
            this.form.check_time = data.check_time
            this.form.task_handle = res.data.task_handle
            this.form.task_img = res.data.task_img
			this.form.id = data.id
            this.form.task_remark = res.data.task_remark
            this.detail.code = res.data.device_code
            this.detail.title = res.data.device_title
            this.detail.class_title = res.data.class_title
            this.detail.fault_title = res.data.fault_title
            this.detail.urgent_name = res.data.urgent_name
            this.detail.create_time = res.data.create_time
            this.detail.user_name = res.data.user_name
            this.detail.admin_name = res.data.admin_name
            this.detail.work_admin_name = res.data.work_admin_name
            this.detail.work_create_time = res.data.work_create_time
            this.detail.maintenance_admin_name = res.data.maintenance_admin_name
            this.detail.maintenance_create_time = res.data.maintenance_create_time
            this.detail.img = res.data.img
		},
        // 人员
        async getHum(){
            var res = await this.$API.all.getAdmin.get({is_page:0});
            this.humList = res.data
        },
	}
}
</script>

<style>
    .title{
        margin: 0 20px 20px;
        color: #01a066;
    }
</style>
